package com.exam.project.business.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.exam.project.business.dto.PracticeDto;
import com.exam.project.business.dto.QuestionDto;
import com.exam.project.business.service.IPracticeRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.exam.framework.aspectj.lang.annotation.Log;
import com.exam.framework.aspectj.lang.enums.BusinessType;
import com.exam.project.business.domain.Question;
import com.exam.project.business.service.IQuestionService;
import com.exam.framework.web.controller.BaseController;
import com.exam.framework.web.domain.AjaxResult;
import com.exam.common.utils.poi.ExcelUtil;
import com.exam.framework.web.page.TableDataInfo;

/**
 * 题目Controller
 * 
 * @author qiuwenbing
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/business/question")
public class QuestionController extends BaseController
{
    @Autowired
    private IQuestionService questionService;

    @Resource
    private IPracticeRecordService practiceRecordService;

    /**
     * 查询题目列表
     */
    @PreAuthorize("@ss.hasPermi('business:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(Question question)
    {
        startPage();
        List<Question> list = questionService.selectQuestionList(question);
        return getDataTable(list);
    }


    @GetMapping("/myList")
    public TableDataInfo myList(QuestionDto question)
    {
        startPage();
        List<QuestionDto> list = questionService.selectMyQuestionList(question);
        return getDataTable(list);
    }
    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('business:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.selectQuestionList(question);
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "题目数据");
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(questionService.selectQuestionById(id));
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('business:question:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Question question)
    {
        return toAjax(questionService.insertQuestion(question));
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('business:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateQuestion(question));
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('business:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.deleteQuestionByIds(ids));
    }

    @GetMapping("markQuestion/{id}")
    public AjaxResult markQuestion(@PathVariable Long id){
        return toAjax(practiceRecordService.markQuestion(id));
    }

    @GetMapping("unMarkQuestion/{id}")
    public AjaxResult unMarkQuestion(@PathVariable Long id){
        return toAjax(practiceRecordService.unMarkQuestion(id));
    }

    @PostMapping("submitPractice")
    public AjaxResult submitPractice(@RequestBody PracticeDto dto){
        return toAjax(practiceRecordService.submitPractice(dto));
    }
}
